package me.siphone.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import me.siphone.model.Measurement;
import me.siphone.model.Station;
import org.linphone.R;

public class StationAdapter extends RecyclerView.Adapter<StationAdapter.StationViewHolder> {

    private List<Station> stationList;

    public StationAdapter(List<Station> stationList) {
        this.stationList = stationList;
    }

    @NonNull
    @Override
    public StationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_station_item, parent, false);
        return new StationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StationViewHolder holder, int position) {
        Station station = stationList.get(position);
        holder.tvNo.setText("站点：" + station.getSn());
        holder.tvName.setText(station.getName().trim());
        int i = 0;
        for (Measurement measurement : station.getMeasurementList()) {
            View measurementView =
                    LayoutInflater.from(holder.itemView.getContext())
                            .inflate(R.layout.layout_measurement_item, holder.gridLayout, false);
            TextView tvType = measurementView.findViewById(R.id.tv_type);
            TextView tvTime = measurementView.findViewById(R.id.tv_time);
            TextView tvLevel = measurementView.findViewById(R.id.tv_level);
            TextView tvStatus = measurementView.findViewById(R.id.tv_status);
            tvType.setText(measurement.getType());
            tvTime.setText("时间：" + measurement.getTime());
            if ("雨量".equals(measurement.getType()) || "暴雨加报".equals(measurement.getType())) {
                tvLevel.setText("雨量：" + measurement.getLevel() + measurement.getUnit());
            } else {
                tvLevel.setText("深度：" + measurement.getLevel() + measurement.getUnit());
            }
            setStatus(tvStatus, measurement.getStatus());

            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.width = 0;
            params.height = GridLayout.LayoutParams.WRAP_CONTENT;
            params.columnSpec = GridLayout.spec(i % 2, 1f);
            params.rowSpec = GridLayout.spec(i / 2, 1f);
            params.rightMargin = 10;
            params.bottomMargin = 10;

            holder.gridLayout.addView(measurementView, params);

            i++;
        }
    }

    private void setStatus(TextView tvStatus, String status) {
        if (status == null) {
            status = "";
        }
        switch (status) {
            case "0":
                tvStatus.setText("状态：正常");
                tvStatus.setTextColor(Color.parseColor("#96c11f"));
                break;
            case "1":
                tvStatus.setText("状态：一级告警");
                tvStatus.setTextColor(Color.parseColor("#2196F3"));
                break;
            case "2":
                tvStatus.setText("状态：二级告警");
                tvStatus.setTextColor(Color.parseColor("#ffab4d"));
                break;
            case "3":
                tvStatus.setText("状态：三级告警");
                tvStatus.setTextColor(Color.parseColor("#e65000"));
                break;
            case "4":
                tvStatus.setText("状态：四级告警");
                tvStatus.setTextColor(Color.parseColor("#ff0000"));
                break;
            default:
                tvStatus.setText("状态：未知");
                tvStatus.setTextColor(Color.parseColor("#808080"));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return stationList.size();
    }

    public void update(List<Station> stationList) {
        this.stationList = stationList;
        notifyDataSetChanged();
    }

    static class StationViewHolder extends RecyclerView.ViewHolder {

        TextView tvNo;
        TextView tvName;
        GridLayout gridLayout;

        public StationViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNo = itemView.findViewById(R.id.tv_station_code);
            tvName = itemView.findViewById(R.id.tv_station_name);
            gridLayout = itemView.findViewById(R.id.measurement_list);
        }
    }
}
